<?php
class Users extends CI_Model{
	
	function validate_user_elderly($data){
		$result = $this->db->get_where('users', array('email'=>$data['email'], 'password'=>$data['password']));	
		
		if($result->num_rows()==1){
			$row = $result->row();
			$user = array(
				'user_id' => $row->user_id,
				'fname' => $row->firstname,
				'lname' => $row->lastname,
				'email' => $row->email
			);
			return $user;
		}
	}
	
	function validate_user_care($data){
		$result = $this->db->get_where('caregivers', array('email'=>$data['email'], 'password'=>$data['password']));	
		
		if($result->num_rows()==1){
			$row = $result->row();
			$user = array(
				'care_id' => $row->care_id,
				'fname' => $row->firstname,
				'lname' => $row->lastname,
				'email' => $row->email
			);
			return $user;
		}
	}
	
	function insert_new_user($data){
		$insert = $this->db->insert('users',$data);
		if($insert){
			$get = $this->get_all_user();
			if($get){
				$new = $get->last_row();
				return $new->user_id;
			}
		}
		else{
			return FALSE;
		}
	}
	
	function get_all_user(){
		$users = $this->db->get('users');
		
		if($users){
			return $users;
		}
		else return FALSE;
	}
	function get_all_care(){
		$users = $this->db->get('caregivers');
		
		if($users){
			return $users;
		}
		else return FALSE;
	}
	
	function get_user($id){
		$user = $this->db->get_where('users', array('user_id'=>$id));
		
		if($user->num_rows() == 1){
			return $user->row();
		}
		else return FALSE;
	}
	
	function get_care($id){
		$care = $this->db->get_where('caregivers', array('care_id'=>$id));
		
		if($care->num_rows() == 1){
			return $care->row();
		}
		else return FALSE;
	}
	
	function get_elder_id($cid){
		$elder = $this->db->get_where('elder_care', array('care_id'=>$cid));
		if($elder->num_rows() == 1){
			$e =  $elder->row();
			return $e->elderly_id;
		}
		else return FALSE;
	}
	
	function get_care_id($eid){
		$care = $this->db->get_where('elder_care', array('elderly_id'=>$eid));
		if($care->num_rows() == 1){
			$c =  $care->row();
			return $c->care_id;
		}
		else return FALSE;
	}
	
	function insert_new_care($care_info){
		$insert = $this->db->insert('caregivers',$care_info);
		if($insert){
			$get = $this->get_all_care();
			if($get){
				$new = $get->last_row();
				return $new->care_id;
			}
		}
		else{
			return FALSE;
		}
	}
	
	function insert_elder_care($info){
		$insert = $this->db->insert('elder_care', $info);
		if($insert) return TRUE;
		else return FALSE;
	}
}